home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 30 / Mac Magazin and MacEasy Magazine CD - Issue 30.iso / utilities / Mac OS X / Load_Monitor / src / LoadInfo.m < prev    next >
Encoding:
Text File  |  2001-12-07  |  1.9 KB  |  99 lines

  1. /*
  2.  *    Load Monitor
  3.  *
  4.  *    Copyright © 2001 Alexandre Vial, some parts from Bernhard Baehr
  5.  *
  6.  *    LoadInfo.m - System load Container Class
  7.  *
  8.  *    This program is free software; you can redistribute it and/or modify
  9.  *    it under the terms of the GNU General Public License as published by
  10.  *    the Free Software Foundation; either version 2 of the License, or
  11.  *    (at your option) any later version.
  12.  *
  13.  *    This program is distributed in the hope that it will be useful,
  14.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *    GNU General Public License for more details.
  17.  *
  18.  *    You should have received a copy of the GNU General Public License
  19.  *    along with this program; if not, write to the Free Software
  20.  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22.  
  23.  
  24. #import "LoadInfo.h"
  25.  
  26. #import "sys/sysctl.h"
  27.  
  28. @implementation MemInfo
  29.  
  30. - (id)initWithCapacity:(unsigned)numItems
  31. {        
  32.     self = [super init];
  33.     size = numItems;
  34.     vmdata = calloc(numItems, sizeof(VMData));
  35.     if (vmdata == NULL) {
  36.         NSLog (@"Failed to allocate buffer for MemInfo");
  37.         return (nil);
  38.     }
  39.     inptr = 0;
  40.     outptr = -1;
  41.         
  42.     return (self);
  43. }
  44.  
  45.  
  46. - (void)refresh
  47. {        
  48.         double loads[3];
  49.  
  50.         getloadavg(loads, 3);
  51.         
  52.         vmdata[inptr].load0 = loads[0];
  53.         vmdata[inptr].load1 = loads[1];
  54.         vmdata[inptr].load2 = loads[2];
  55.  
  56.     if (++inptr >= size)
  57.         inptr = 0;
  58. }
  59.  
  60.  
  61. - (void)startIterate
  62. {
  63.     outptr = inptr;
  64. }
  65.  
  66.  
  67. - (BOOL)getNext:(VMDataPtr)ptr
  68. {
  69.     if (outptr == -1)
  70.         return (FALSE);
  71.     *ptr = vmdata[outptr++];
  72.     if (outptr >= size)
  73.         outptr = 0;
  74.     if (outptr == inptr)
  75.         outptr = -1;
  76.     return (TRUE);
  77. }
  78.  
  79.  
  80. - (void)getCurrent:(VMDataPtr)ptr
  81. {
  82.     *ptr = vmdata[inptr ? inptr - 1 : size - 1];
  83. }
  84.  
  85.  
  86. - (void)getLast:(VMDataPtr)ptr
  87. {
  88.     *ptr = vmdata[inptr > 1 ? inptr - 2 : size + inptr - 2];
  89. }
  90.  
  91.  
  92. - (int)getSize
  93. {
  94.     return (size);
  95. }
  96.  
  97.  
  98. @end
  99.